<template>
  <el-dialog
    :title="'Add payment certificate'"
    :visible.sync="dialogVisible"
    width="100%"
    :fullscreen="true"
    :modal="false"
    modal-append-to-body
    :before-close="handleClose"
    :destroy-on-close="true"
    :append-to-body="true"
  >
    <div class="xlSeeFrom">

      <div class="tab-item">
        <!--审核完成报销单-->
        <div>
          <span class="tit-header">
            <div class="item">
              <p class="tab-title">Expense Account Audited</p>
            </div>
            <div class="item">
                <div class="search">
                    <div>
                        <el-input v-model="searchReName" placeholder="please enter" style="width:100%">
                            <el-button slot="append" @click="searchRe">search</el-button>
                            <el-button slot="append" @click="clearReSearch">cancel search</el-button>
                        </el-input>
                    </div>
                </div>
            </div>
          </span>

          <div class="tab">
            <el-table
              :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
              :data="reimbursementData"
              ref="reimbursementData"
              height="330px"
              tooltip-effect="dark"
              border
              style="width:100%"
              @selection-change='selectRe'>
              <el-table-column type="selection" width="45" align="center"></el-table-column>
              <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
              <el-table-column
              label="ID"
              align="center"
              prop="expense_id">
              </el-table-column>
             <el-table-column
              label="Name"
              align="center"
              prop="expense_name">
              </el-table-column>
              <el-table-column
              label="Applicant"
              align="center"
              prop="expense_pe_name">
              </el-table-column>
              <el-table-column
              label="Submit Time"
              align="center"
              prop="expense_submit_time">
              </el-table-column>
             <el-table-column
              label="Status"
              align="center"
              prop="status">
              </el-table-column>
            </el-table>
          </div>
        </div>

        <!--审核完成工资单-->
        <div>
          <span class="tit-header">
            <div class="item">
              <p class="tab-title">Payroll Audited</p>
            </div>
            <div class="item">
                <div class="search">
                  <div class="dateSearch">
                      <el-date-picker
                        v-model="form.startTime"
                        type="month"
                        value-format='yyyy-MM'
                        :picker-options="startTime"
                        placeholder="strat time">
                      </el-date-picker>
                      <el-date-picker
                        v-model="form.endTime"
                        type="month"
                        value-format='yyyy-MM'
                        :picker-options="endTime"
                        placeholder="end time">
                      </el-date-picker>
                  </div>
                  <div>
                      <el-button type="info" plain @click="searchPay" style="border-radius:0;">search</el-button>
                      <el-button type="info" plain @click="clearPaySearch" style="margin-left:0px;border-radius:0;">cancel search</el-button>
                  </div>
                </div>
            </div>
          </span>

          <div class="tab">
            <el-table
              :header-cell-style="{background:'#F2F6FC',color:'#606266'}"
              :data="salaryData"
              ref="salaryData"
              height="330px"
              tooltip-effect="dark"
              border
              style="width:100%"
              @selection-change='selectPay'>
              <el-table-column type="selection" width="45" align="center"></el-table-column>
              <el-table-column label="S/N" type="index" width="60" align="center"></el-table-column>
              <el-table-column
              label="Payroll ID"
              align="center"
              prop="salary_id">
              </el-table-column>
              <el-table-column
              label="Belong Date"
              align="center"
              prop="belong_date">
              </el-table-column>
              <el-table-column
              label="Submit Time"
              align="center"
              prop="submit_time">
              </el-table-column>
             <el-table-column
              label="Status"
              align="center"
              prop="status">
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>

    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">Close</el-button>
      <el-button type="primary" @click="createPayment">Confirm</el-button>
    </span>

    <editForm ref="editForm"></editForm>
  </el-dialog>


</template>

<script>
import {getAuditedReimbursement,getAuditedSalary,createPayment} from '@/request/other_api'
import editForm from "./editForm";


export default {
  components:{
    editForm
  },
  data(){
    return {
        dialogVisible:false,
        //审核完成报销单数据
        reimbursementData:[],
        //审核完成工资单数据
        salaryData:[],
        //审核完成报销单搜索字段
        searchReName:'',
        //审核完成工资单字段
        searchSalaryName:'',
        //搜索时间
        form:{
          startTime:undefined,
          endTime:undefined,
        },
        startTime: {
          disabledDate: time => {
            let endDateVal = this.form.endTime;
            if (endDateVal) {  return time.getTime() > new Date(endDateVal).getTime()}
          }
        },
        endTime: {
          disabledDate: time => {
            let beginDateVal = this.form.startTime;
            if (beginDateVal) { return time.getTime() < new Date(beginDateVal).getTime()}
          }
        },
        //选中的报销单
        selectlistRe:[],
        //选中的工资单
        selectlistPay:[],
    }
  },
  mounted () {
    this.level = sessionStorage.getItem('level')
  },
  methods:{
    //获取审核完成报销单列表
    formateData(){
        getAuditedReimbursement().then(res =>{
            if(res.code == 200){
                this.reimbursementData = res.data
            }
        })
        getAuditedSalary().then(res =>{
            if (res.code == 200) {
                this.salaryData = res.data
            }
        })
    },
    //搜索报销单
    searchRe(){
      getAuditedReimbursement({
        keyword:this.searchReName
      }).then(res =>{
        this.reimbursementData = res.data
      })
    },
    //取消报销单搜索
    clearReSearch(){
      getAuditedReimbursement().then(res =>{
        if(res.code == 200){
            this.reimbursementData = res.data
            this.searchReName = ''
        }
      })
    },
    // 搜索工资单
    searchPay(){
      getAuditedSalary({
        startDate:this.form.startTime,
        endDate:this.form.endTime,
      }).then(res =>{
        this.salaryData = res.data
      })
    },
    // 取消搜索工资单
    clearPaySearch(){
      getAuditedSalary().then(res =>{
        if (res.code == 200) {
            this.salaryData = res.data
        }
      })
    },
    //获取选中的报销单
    selectRe (val) {
      this.selectlistRe = val
    },
    //获取选中的工资单
    selectPay (val) {
      this.selectlistPay = val
    },
    createPayment() {
      if (this.selectlistRe.length == 0 && this.selectlistPay.length == 0) {
        this.$message.error("Please select at least one reimbursement slip or pay slip!")
        return
      }
      let expense_id = []
      this.selectlistRe.forEach(item => {
        expense_id.push(item.expense_id)
      })
      let salary_id = []
      this.selectlistPay.forEach(item => {
        salary_id.push(item.salary_id)
      })
      createPayment({
        expense_id:expense_id,
        salary_id:salary_id,
      }).then(res =>{
        if (res.code == 200) {
          this.$message.success("Successfully created payment voucher!")
          this.$parent.seePaymentList()

          let payment_voucher_id = res.data[0].certificate_number

          this.$refs.editForm.seePaymentDetail(payment_voucher_id,1)
          this.$refs.editForm.dialogVisible = true

          // this.dialogVisible = false
        }
      })
    },
    //关闭窗口
    handleClose (data) {
      if(data == 1){
        this.$parent.seePaymentList()
      }
      this.dialogVisible = false
    },
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label {
  background-color: #eee;
  text-align: center;
  //width: 121px;
}
/deep/ .el-dialog__header {
  padding: 20px 20px 10px;
  border-bottom: 1px solid #eee;
}
/deep/ .el-dialog__body {
  padding: 10px 20px;
  color: #333;
  font-size: 15px;
  word-break: break-all;
}
.step{
  margin-bottom: 10px;
}
.content{
  margin-top: 10px;
  margin-bottom: 10px;

}
.tab-item{
  width: 100%;
  padding: 10px 0;
  border: 1px solid #eee;

  table{
    width: 100%;
    font-family: verdana,arial,sans-serif;
    font-size:14px;
    color:#333333;
    border-width: 1px;
    border-color: #999999;
    border-collapse: collapse;
  }
  table td{
    border-width: 1px;
    padding: 4px;
    border-style: solid;
    border-color: #999999;
    width: 25%;
  }
  table tr:nth-child(2n+1){
    background-color:#F2F6FC;
    height:50px;
    text-align: center;
  }
  table tr:nth-child(2n){
    background-color: #fff;
    height:50px;
    text-align: center;
  }

.tab-title{
  font-family: SourceHanSansSC;
  font-weight: 700;
  font-size: 20px;
  color: rgb(16, 16, 16);
  font-style: normal;
  letter-spacing: 0px;
  line-height: 30px;
  text-decoration: none;
  padding: 5px 10px;
}
/deep/ .el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 100%;
 }
}

.tit-header{
    display: flex;
    justify-content: space-between;
    margin: 10px;
}
.search {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
}
.dateSearch {
  display: flex;
  justify-content: space-between;
  padding-bottom: 0px;
}
</style>
